﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.DynamicData;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using NotAClue;
using NotAClue.Web;
using System.Collections;

namespace NotAClue.Web
{
    public static class GridViewRolloverEffectExtensionMethods
    {
        private static CommandButtonType _commandButtonType;
        private static String _hightlightId = ".DDGridView";
        private static String _hightlightClass = "highlight";
        private static readonly String _rolloverScript;

        public static CommandButtonType CommandButtonType
        {
            get { return _commandButtonType; }
            set { _commandButtonType = value; }
        }

        public static String HightlightClass
        {
            get { return _hightlightClass; }
            set { _hightlightClass = value; }
        }

        public static String HightlightId
        {
            get { return _hightlightId; }
            set { _hightlightId = value; }
        }

        static GridViewRolloverEffectExtensionMethods()
        {
            //var rolloverScript = new StringBuilder();
            //rolloverScript.Append("\n");
            //rolloverScript.Append("$(\"{0}\").delegate('TR','mouseover mouseleave', function(e) {{\n");
            //rolloverScript.Append("    if (e.type == 'mouseover') {{\n");
            //rolloverScript.Append("      $(this).addClass(\"{1}\");\n");
            //rolloverScript.Append("    }}\n");
            //rolloverScript.Append("    else {{\n");
            //rolloverScript.Append("      $(this).removeClass(\"{1}\");\n");
            //rolloverScript.Append("    }}\n");
            //rolloverScript.Append("}});\n");

            //_rolloverScript = String.Format(rolloverScript.ToString(), _hightlightId, _hightlightClass);

            var changeColorScript = new StringBuilder();

            //build change ChangeColorScript
            changeColorScript.Append("var NAC_LastColorUsed;\n");
            changeColorScript.Append("function NAC_ChangeBackColor(row, highlight, RowHighlightColor)\n");
            changeColorScript.Append("{\n");
            changeColorScript.Append("    if (highlight)\n");
            changeColorScript.Append("    {\n");
            changeColorScript.Append("        // set the background color\n");
            changeColorScript.Append("        NAC_LastColorUsed = row.style.backgroundColor;\n");
            changeColorScript.Append("        row.style.backgroundColor = RowHighlightColor;\n");
            changeColorScript.Append("    }\n");
            changeColorScript.Append("    else\n");
            changeColorScript.Append("    {\n");
            changeColorScript.Append("        // restore the color\n");
            changeColorScript.Append("        row.style.backgroundColor = NAC_LastColorUsed;\n");
            changeColorScript.Append("    }\n");
            changeColorScript.Append("}\n");

            _rolloverScript = changeColorScript.ToString();

            // set default value
            _commandButtonType = Web.CommandButtonType.Select;
        }

        public static void AddRolloverEffect(this GridView gridView, Boolean useCustomOnDataBoundEvent = false)
        {
            //// add change color script
            //gridView.Page.AddStartupClientScript("NAC_RolloverScript", _rolloverScript);

            // add change color script
            gridView.Page.AddClientScript("NAC_RolloverScript", _rolloverScript);

            switch (_commandButtonType)
            {
                case CommandButtonType.Edit:
                    gridView.AutoGenerateEditButton = true;
                    break;
                case CommandButtonType.Delete:
                    gridView.AutoGenerateDeleteButton = true;
                    break;
                case CommandButtonType.Select:
                default:
                    // add select button
                    gridView.AutoGenerateSelectButton = true;
                    break;
            }

            // add data bound method
            if (!useCustomOnDataBoundEvent)
                gridView.DataBound += GridView_DataBound;

            // add row data bound method
            gridView.RowDataBound += GridView_RowDataBound;

            //gridView.Page.PreRenderComplete += Page_PreRenderComplete;
        }

        public static void AddRolloverEffectScriptValidation(this GridView gridView)
        {
            // The client scripts for gvReleased were created in gvReleased_RowDataBound
            foreach (GridViewRow row in gridView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    var selectButton = row.Cells[0].FindControlRecursive<LinkButton>();
                    if (selectButton != null && String.Compare(selectButton.CommandName, "Select", true) == 0)
                        gridView.Page.ClientScript.RegisterForEventValidation(selectButton.UniqueID);
                }
            }
        }

        public static void GridView_DataBound(object sender, EventArgs e)
        {
            var gridView = (GridView)sender;
            var page = gridView.Page;

            foreach (GridViewRow row in gridView.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    // Add java script to highlight row
                    row.Attributes["onmouseover"] = "javascript:NAC_ChangeBackColor(this, true, '#BAD5E8'); this.style.cursor = 'pointer';";
                    row.Attributes["onmouseout"] = "javascript:NAC_ChangeBackColor(this, false, '');";

                    var selectButton = row.Cells[0].FindControlRecursive<LinkButton>();
                    if (selectButton != null && String.Compare(selectButton.CommandName, "Select", true) == 0)
                    {
                        // Get the java script which is assigned to this LinkButton
                        String jsClick = page.ClientScript.GetPostBackClientHyperlink(selectButton, "");
                        // add script to row
                        row.Attributes["onclick"] = jsClick;
                    }
                }
            }
        }

        public static void GridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType != DataControlRowType.EmptyDataRow && e.Row.RowType != DataControlRowType.Pager)
                e.Row.Cells[0].Visible = false;
        }
    }
}
